package concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class FutureTaskDemo {

    static int count = 10;

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ExecutorService executorService = Executors.newCachedThreadPool();

        List<FutureTask<Integer>> futureTasks = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            int finalI = i;
            FutureTask<Integer> futureTask = new FutureTask<>(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    return finalI;
                }
            });

            new Thread(futureTask).start();
//            executorService.submit(futureTask);
            futureTasks.add(futureTask);
        }

        StringBuffer result = new StringBuffer();
        for (FutureTask<Integer> futureTask : futureTasks) {
            result.append(futureTask.get());
        }
        System.out.println(result);

    }
}
